import React from 'react'
// ===================================================================== Temp
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== private component
const Select 			= $Lazy.load(()=>import('@antd/form/select'))
const confirm 			= $Lazy.file(()=>import('@antd/confirm'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
const AddDicModal 		= $Lazy.state(()=>import('./add-dic-modal'))
// ===================================================================== data
const proStatus = [ { name: "已审核", value: "1" },{ name: "待审核", value: "0" },{ name: "待保存", value: "-2" }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		specType:[]
	}
	
	componentDidMount(){
		this.getSelect()
	}
	getSelect = () => {
		$cache.then( async f => {
			// 标本类型
			const specType = $fn.setRow( this.cols, 'spec_type', await f.getDicCache({ code: 2000, refresh:true }) )
			this.setState({ specType })
		})
	}
	
	fetch = (param,remSpecType) => {
		const { uuid, spec_code } = this.props.specData
		const kindParam = param ? {...param, double_status:this.props.status} : { spec_id:uuid,spec_code,double_status:this.props.status }
		const { memoryData } = this.props
		$http.pull(this,'specimen-kind-record/index', { param:kindParam }).then(data=>{
			if(remSpecType && $fn.hasArray(memoryData)){
				let specTypeP = []
				memoryData.forEach((v1,i1)=>{
					this.state.data.forEach(v2=>{
						if(v1.kind_code === v2.kind_code && v1.spec_type!==v2.spec_type){
							specTypeP.push({spec_type:v1.spec_type,uuid:v2.uuid})
						}
					})
				})
				if($fn.hasArray(specTypeP)){
					$http.submit(null,'specimen-kind/savespectype',{ param:{param:specTypeP,double_status:this.props.status}, loadingText:'同步标本类型中' }).then(data=>{
						this.fetch()
					})
				}
				
			}
		})
	}
	
	reset = () => {
		this.setState({data:[]})
	}

	cols=[
		{ title: '项目代码', 	dataIndex: 'kind_code',				...$fn.table.exp_num,	 },
		{ title: '项目名称', 	dataIndex: 'kind_name', 			width:180 },
		{ title: '岗位 ', 		dataIndex: 'project_name',  		width:120,  },
		{ title: '标本类型 ', 	dataIndex: 'spec_type_name', 		width:150,	name:'spec_type', 	render: (text, {uuid}) => {
			return <Select
				name		= 'spec_type'
				value		= {text}  
				data		= {this.state.specType}
				width		= '100%'
				outer
				tipTxt		= ' '
				onChange 	= { value  =>{
					const { specType } = this.state
					confirm.then(f=>{
						const spec_type = value ? specType[value].code : ''
						$http.submit(null,'specimen-kind/savespectype',{ param:{spec_type,uuid,double_status:this.props.status}, loadingText:`修改中`}).then(data=>{
							$fn.msg.success(`操作成功`)
							this.fetch()
						})
					})
				}}
			/>
		}},
		{ title: '项目属性 ', 	dataIndex: 'kind_type_name',  		width:120,  },
		{ title: '项目类别 ', 	dataIndex: 'category_name',  		width:120,  },
		// { title: '子项目 ', 		dataIndex: 'child_kind_name',  		width:300,  },
		{ title: '项目状态 ', 	dataIndex: 'enabled',  				width:100,	render: text => $fn.getState(proStatus,text)  },
		TableControls.controls.call(this,{ width: 100 })
	]	
	// 表格操作按钮
	table_controls = ({uuid,spec_id,enabled,is_sg_check}) => {
		return [
			TableControls.click.call(this, '添加', ()=>{
				this.addDicModal.open()
			}),
			+enabled !== 1?Confirm.call(this, '删除', 'specimen-kind-record/del',{
				msg: +is_sg_check ? '该项目为关联项目谨慎删除，确认删除？' : '确认删除？',
				query:{uuid},
				onOk:()=> {this.fetch()}
			}):null,	// 删除
		]
	}	

	render(){
		return (
			<>
				{Table.call(this)} 
				{/* 添加字典 */}
				<AddDicModal onRef = { ref => this.addDicModal = ref } onRefresh = {this.getSelect} />
			</>
		)
	}
}

export default Index